package com.stdspringboot.service.impl;

import com.stdspringboot.entity.User;
import com.stdspringboot.mapper.IUserMapper;
import com.stdspringboot.service.IRegService;
import com.stdspringboot.vo.JsonResult;
import com.stdspringboot.vo.UserVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service
public class RegSrvImpl implements IRegService {

    //在该类中，用谁，将把谁声明为成员变量，并加上@Resource注解
    @Resource
    private IUserMapper mapper;

    public JsonResult reg(UserVO userVO) throws Exception {

        JsonResult jr=null;
        //做校验，用户名是否存在
        int count=mapper.queryCountByUsername(userVO.getUsername());

        if(count>0){
            jr=new JsonResult(JsonResult.CODE_USERNAME_EXIST,"登录名已经存在，请重新填写！");
            return jr;
        }

        //要实现存储，就需要将 UserVO的值赋给 实体类User，
        // 然后调用mapper接口保存

        String uuid= UUID.randomUUID().toString().replace("-","");
        User user=new User();
        user.setUid(uuid);
        user.setUsername(userVO.getUsername());
        user.setPassword(userVO.getPassword());
        user.setJiguan(userVO.getJiguan());
        user.setJieshao(userVO.getJieshao());


//        user.setLikes(userVO.getLikes());
        String[] likes = userVO.getLikes();
        StringBuilder likeStr=new StringBuilder();//cj,dy,wzry，用逗号分隔开
        String likeDb="";
        if(likes!=null){
            for(String item  :  likes){
                likeStr.append(item+",");
            }
            //字符串截取，会返回一个字符串
            likeDb=likeStr.substring(0, likeStr.length()-1);
        }
        user.setLikes(likeDb);

        user.setPhone(userVO.getPhone());
        user.setRealName(userVO.getRealName());
        user.setSex(userVO.getSex());

        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String datetime=sdf.format(date);
        user.setCreateTime(datetime);
        user.setModifyTime(datetime);


        int i=mapper.save(user);
        System.out.println("影响的行数："+i);


        return null;
    }
}
